package com.bls.cate.test.Base;

import com.bls.cate.test.Base.BaseModelCallback.RequestNetState;
import com.bls.cate.test2.base.BaseView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

/**
 * 　　  () 　　　  ()
 * 　　  ( ) 　　　( )
 * 　　  ( ) 　　　( )
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ——————————————————
 * Created by BinglingSu on 2018/5/19.
 */
public class BaseModelImpl implements BaseModel {
    @Override
    public void requestGetNetData(final int requestId, String url, CacheMode cacheMode, final BaseModelCallback callback, HttpParams... httpParamsArray) {
        String cacheKey;
        HttpParams httpParams = new HttpParams();
        cacheKey = url.concat(httpParams.toString());

        if (httpParamsArray != null)
            for (HttpParams h : httpParamsArray) {
                httpParams.put(h);
            }

        OkGo.<String>get(url).tag(url).cacheKey(cacheKey).cacheMode(cacheMode).params(httpParamsArray == null ? null : httpParams).execute(new StringCallback() {
            @Override
            public void onStart(Request<String, ? extends Request> request) {
                super.onStart(request);
                callback.onRequestNetResult(requestId, RequestNetState.onStart, null, null);
            }

            @Override
            public void onSuccess(Response<String> response) {
                callback.onRequestNetResult(requestId, RequestNetState.onSuccess, response.body(), response);
            }

            @Override
            public void onCacheSuccess(Response<String> response) {
                super.onCacheSuccess(response);
                onSuccess(response);
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                callback.onRequestNetResult(requestId, RequestNetState.onError, response.body(), response);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                callback.onRequestNetResult(requestId, RequestNetState.onFinish, null, null);
            }
        });
    }



/*    @Override
    public void requestNetData(int requestId, String url, CacheMode cacheMode, final OnRequestNetListener listener, HttpParams... httpParamsArray) {
        String cacheKey;
        HttpParams httpParams = new HttpParams();
        for (HttpParams h : httpParamsArray) {
            httpParams.put(h);
        }
        cacheKey = url.concat(httpParams.toString());
        OkGo.<String>get(url).tag(url).cacheKey(cacheKey).cacheMode(cacheMode).params(httpParams).execute(new StringCallback() {
            @Override
            public void onStart(Request<String, ? extends Request> request) {
                listener.OnRequestNetStart();
                super.onStart(request);
            }

            @Override
            public void onSuccess(Response<String> response) {
                listener.OnRequestNetSuccess();
            }

            @Override
            public void onCacheSuccess(Response<String> response) {
                super.onCacheSuccess(response);
            }

            @Override
            public void onError(Response<String> response) {
                listener.OnRequestNetError();
                super.onError(response);
            }

            @Override
            public void onFinish() {
                listener.OnRequestNetFinish();
                super.onFinish();
            }
        });
    }*/
}
